
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

void debug_i();

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    //char *request = "GET / HTTP/1.1 \r\nHost: www.baidu.com\r\nConnection: Upgrade";
    //char *request = "get";
    if (argc < 2) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    } else if(argc >= 3) {
        portno = atoi(argv[2]);
    } else {
        portno = 80;
    }


    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;

    bcopy((char *)server->h_addr,
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);


//    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    serv_addr.sin_port = htons(portno);

    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
        error("ERROR connecting");
    printf("Please enter the message: ");

    char pWrite[512];
	memset(pWrite, 0, sizeof(pWrite));

	//sprintf(pWrite, "GET http://%s/ HTTP/1.1\nAccept: */*\n"
	//	"Accept-Language: en-us\n"
	//	"Accept-Encoding: deflate\n"
	//	"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0)\n"
	//	"Host: %s\n"
	//	"Connection: close\n\n", argv[1], argv[1]);

    sprintf(pWrite, "GET http://%s/ HTTP/1.1\n"
                    "Host: %s\n"
                    "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:2.0) Gecko/20100101 Firefox/4.0\n"
                    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n"
                    "Accept-Language: en-us,en;q=0.5\n"
                    //"Accept-Encoding: gzip, deflate\n"
                    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n"
                    "Keep-Alive: 115\n"
                    "Proxy-Connection: keep-alive\n"
                    "Cookie: PHPSESSID=beobp2prj5qk99obsod0jk9qj0\n"
                    "Cache-Control: max-age=0\nConnection: close\n\n", argv[1], argv[1]);
    puts(pWrite);

    n = send(sockfd, pWrite, strlen(pWrite), 0);
    puts("ww\n");
    //n = write(sockfd,request,strlen(buffer));
    //n = send(sockfd, request, strlen(request), 0);

   // debug_i();
    if (n < 0)
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n < 0)
         error("ERROR reading from socket");
    printf("%s\n",buffer);
    while((n = read(sockfd,buffer,255)) > 0)
    {
        printf("%s",buffer);
        bzero(buffer,256);
    }
    close(sockfd);
    puts("\n\n\r");
    return 0;
}

void debug_i()
{
    printf("test \r\n");
}

